//
//  FirstViewController.m
//  TestApp
//
//  Created by Ondrej Rafaj on 26.10.09.
//  Copyright Home 2009. All rights reserved.
//

#import "FirstViewController.h"

@implementation FirstViewController

@synthesize textView;

- (void) printResults:(NSString *)text {
	counter++;
	textView.text = [NSString stringWithFormat:@"%d) %@\r\n\r\n%@", counter, text, textView.text];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	counter = 0;
	SQLite3Connector *conn = [[SQLite3Connector alloc] init];
	//[conn getData:@"" fromDatabase:@""];
	NSString *queryCount = @"SELECT COUNT(id) FROM testTable;";
	[self printResults:[NSString stringWithFormat:@"Directory path: %@", [SQLite3Connector getDocumentsDirectoryPath]]];
	[self printResults:[NSString stringWithFormat:@"Database folder path: %@", [SQLite3Connector getDatabaseFolderPath]]];
	[self printResults:[NSString stringWithFormat:@"Database file name: %@", [SQLite3Connector getDatabasePathFromName:@"testDatabase"]]];
	[self printResults:[NSString stringWithFormat:@"Escape string (it's): %@", [SQLite3Connector escapeString:@"it's"]]];
	//[self printResults:[NSString stringWithFormat:@"Dropping database: %d", [SQLite3Connector dropDatabase:@"testDatabase"]]];
	[self printResults:[NSString stringWithFormat:@"Database exists: %d", [SQLite3Connector databaseExists:@"testDatabase"]]];
	NSString *query = @"CREATE TABLE IF NOT EXISTS testTable (id INTEGER PRIMARY KEY AUTOINCREMENT, data TEXT);";
	[self printResults:[NSString stringWithFormat:@"Creating table: %d", [conn executeQuery:query inDatabase:@"testDatabase"]]];
	[self printResults:[NSString stringWithFormat:@"Database size: %@", [SQLite3Connector getDatabaseFormatedFileSize:@"testDatabase"]]];
	//[self printResults:[NSString stringWithFormat:@"Dropping table: %d", [conn dropTable:@"testTable" inDatabase:@"testDatabase" useVacuum:YES]]];
	//[self printResults:[NSString stringWithFormat:@"Creating table: %d", [conn executeQuery:query inDatabase:@"testDatabase"]]];
	//[self printResults:[NSString stringWithFormat:@"Database size: %@", [SQLite3Connector getDatabaseFormatedFileSize:@"testDatabase"]]];
	//[self printResults:[NSString stringWithFormat:@"Database exists: %d", [SQLite3Connector databaseExists:@"testDatabase"]]];
	query = @"INSERT INTO testTable (data) VALUES ('Test Data (It''s)');";
	[self printResults:[NSString stringWithFormat:@"Inserting into the table: %d", [conn executeQuery:query inDatabase:@"testDatabase"]]];
	//[self printResults:[NSString stringWithFormat:@"Truncate table: %@", [conn truncateTable:@"testTable" inDatabase:@"testDatabase"]]];
	[self printResults:[NSString stringWithFormat:@"Inserting into the table: %d", [conn executeQuery:query inDatabase:@"testDatabase"]]];
	[self printResults:[NSString stringWithFormat:@"Database size: %@", [SQLite3Connector getDatabaseFormatedFileSize:@"testDatabase"]]];
	[self printResults:[NSString stringWithFormat:@"Inserting into the table: %d", [conn executeQuery:query inDatabase:@"testDatabase"]]];
	[self printResults:[NSString stringWithFormat:@"Table exists: %d", [conn tableExists:@"testTable" inDatabase:@"testDatabase"]]];
	[self printResults:[NSString stringWithFormat:@"Database size: %@", [SQLite3Connector getDatabaseFormatedFileSize:@"testDatabase"]]];
	[self printResults:[NSString stringWithFormat:@"Table exists: %d", [conn tableExists:@"testTable" inDatabase:@"testDatabase"]]];
	[self printResults:[NSString stringWithFormat:@"Count rows in the table: %d", [conn executeScalar:queryCount inDatabase:@"testDatabase"]]];
	
	NSMutableArray *arr = [conn getData:@"SELECT * FROM testTable;" fromDatabase:@"testDatabase"];
	NSLog(@"%@", arr);
	
	[conn release];
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[textView release];
    [super dealloc];
}

@end
